package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.supplier.DebtHuan;
import com.sand.dto.supplier.DebtSelecte;
import com.sand.entity.sell.Debt;
import com.sand.entity.sell.DebtDetail;
import com.sand.entity.sell.Purchase;
import com.sand.feign.supplierFeign;
import com.sand.mapper.DebtMapper;
import com.sand.result.AppHttpCodeEnum;
import com.sand.service.DebtDetailService;
import com.sand.service.DebtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class DebtServiceImpl extends ServiceImpl<DebtMapper, Debt> implements DebtService {

    @Autowired
    private supplierFeign supplierFeign;

    @Autowired
    private DebtDetailService debtDetailService;

    /**
     * 添加货款债务
     *
     * @param debt
     * @return
     */
    @Override
    public Integer add(Debt debt) {

        pan(debt);

        save(debt);

        return 1;
    }

    /**
     * 还财务
     *
     * @param debtHuan
     */
    @Override
    @Transactional
    public Integer huan(DebtHuan debtHuan) {

        if (debtHuan.getDebtid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入货款债务表");
        }

        Debt d = getById(debtHuan.getDebtid());

        if (d == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"货款债务表不存在");
        }

        if (d.getWhether() == 1){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"这个货款债务已经还清");
        }

        if (debtHuan.getMoney() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入还款");
        }

        if (debtHuan.getMoney() < 0){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"还款不能为负数");
        }

        //添加还款记录
        DebtDetail debtDetail = new DebtDetail();

        debtDetail.setDebtid(d.getId());
        debtDetail.setMerchantid(d.getMerchantid());
        debtDetail.setSupplierid(d.getSupplierid());
        debtDetail.setMoney(debtHuan.getMoney());
        debtDetail.setHuantime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        debtDetailService.save(debtDetail);

        //判断是否还清
        if (debtHuan.getMoney() >= d.getNomoney()){
            //还清
            d.setWhether(1);
        }

        //没有还清
        d.setNomoney(d.getNomoney()-debtHuan.getMoney());

        //修改债务信息
        updateById(d);

        return 1;
    }

    /**
     * 条件查询货款债务
     *
     * @param debtSelecte
     * @return
     */
    @Override
    public List<Debt> all(DebtSelecte debtSelecte) {

        LambdaQueryWrapper<Debt> wrapper = new LambdaQueryWrapper<>();

        if (debtSelecte.getMerchantid() != null){
            wrapper.eq(Debt::getMerchantid,debtSelecte.getMerchantid());
        }

        if (debtSelecte.getSupplierid() != null){
            wrapper.eq(Debt::getSupplierid,debtSelecte.getSupplierid());
        }

        if (debtSelecte.getWhether() != null){
            wrapper.eq(Debt::getWhether,debtSelecte.getWhether());
        }


        return list(wrapper);
    }

    //添加货款在屋判断
    private void pan(Debt debt) {

        if (debt.getMerchantid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入收货人");
        }

        if (debt.getSupplierid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入发货人");
        }

        if (debt.getPurchaseid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入货款订购单");
        }

        Purchase data = supplierFeign.gid(debt.getPurchaseid()).getData();

        if (data == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该货款订购单不存在");
        }

        if (debt.getMoney() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入货款债务");
        }

        if (debt.getNomoney() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入未付清金额");
        }

        if (debt.getWhether() == null){
            //默认没有还清货款
            debt.setWhether(0);
        }


    }


}
